﻿// <copyright file="ColorConverters.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2009 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-06-08</date>
// <summary>OpenWPFChart library.
// Misc Color Converters.</summary>
// <revision>$Id$</revision>

using System;
using System.Windows.Data;
using System.Windows.Media;

namespace OpenWPFChart.Helpers.ChooseColorDialog
{
	/// <summary>
	/// Coverts the <see cref="System.Windows.Media.Color"/> to <see cref="ovp.WPFGears.HSL"/>
	/// and vice versa.
	/// </summary>
	[ValueConversion(typeof(Color), typeof(HSL))]
	public class ColorToHSLConverter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// Coverts the <see cref="System.Windows.Media.Color"/> to <see cref="ovp.WPFGears.HSL"/>.
		/// </summary>
		/// <param name="value">The value produced by the binding source.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">Not used.</param>
		/// <param name="culture">Not used.</param>
		/// <returns><see cref="ovp.WPFGears.HSL"/> value.</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null || value.GetType() != typeof(Color))
				return value;

			return new HSL((Color)value);
		}

		/// <summary>
		/// Coverts the <see cref="ovp.WPFGears.HSL"/> to <see cref="System.Windows.Media.Color"/>.
		/// </summary>
		/// <param name="value">The value that is produced by the binding target.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">Not used.</param>
		/// <param name="culture">Not used.</param>
		/// <returns><see cref="System.Windows.Media.Color"/> value.</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null || value.GetType() != typeof(HSL))
				return value;

			return (Color)((HSL)value);
		}
		#endregion IValueConverter Members
	}

	/// <summary>
	/// Sets the <see cref="System.Windows.Media.Color"/> Luminance to the value specified
	/// in the <paramref name="parameter"/>.
	/// </summary>
	[ValueConversion(typeof(Color), typeof(Color))]
	public class ColorLuminanceSetter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// Sets the <see cref="System.Windows.Media.Color"/> Luminance to the value specified
		/// in the <paramref name="parameter"/>.
		/// </summary>
		/// <param name="value">The value produced by the binding source.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">New Luminance value.</param>
		/// <param name="culture">Not used.</param>
		/// <returns>New <see cref="System.Windows.Media.Color"/> value with the Luminance specified
		/// in the <paramref name="parameter"/>.</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null || value.GetType() != typeof(Color) || parameter == null)
				return value;
			try
			{
				double newLuminance = (double)System.Convert.ChangeType(parameter, typeof(double));
				if (newLuminance >= 0.0 && newLuminance <= 1.0)
				{
					HSL hsl = new HSL((Color)value);
					hsl.Luminance = newLuminance;
					return (Color)hsl;
				}
			}
			catch
			{
			}
			return value;
		}

		/// <summary>
		/// Converts a value.
		/// </summary>
		/// <param name="value">The value that is produced by the binding target.</param>
		/// <param name="targetType">The type to convert to.</param>
		/// <param name="parameter">The converter parameter to use.</param>
		/// <param name="culture">The culture to use in the converter.</param>
		/// <returns>
		/// A converted value. If the method returns null, the valid null value is used.
		/// </returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}
		#endregion IValueConverter Members
	}

	/// <summary>
	/// Gets the <see cref="System.Windows.Media.Color"/> Luminance value.
	/// </summary>
	[ValueConversion(typeof(Color), typeof(double))]
	public class ColorLuminanceGetter : IValueConverter
	{
		#region IValueConverter Members
		/// <summary>
		/// Gets the <see cref="System.Windows.Media.Color"/> Luminance value.
		/// </summary>
		/// <param name="value">The value produced by the binding source.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">Not used.</param>
		/// <param name="culture">Not used.</param>
		/// <returns>The <see cref="System.Windows.Media.Color"/> Luminance value.</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null || value.GetType() != typeof(Color))
				return value;
			HSL hsl = new HSL((Color)value);
			return hsl.Luminance;
		}

		/// <summary>
		/// Converts a value.
		/// </summary>
		/// <param name="value">The value that is produced by the binding target.</param>
		/// <param name="targetType">The type to convert to.</param>
		/// <param name="parameter">The converter parameter to use.</param>
		/// <param name="culture">The culture to use in the converter.</param>
		/// <returns>
		/// A converted value. If the method returns null, the valid null value is used.
		/// </returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}
		#endregion IValueConverter Members
	}
}
